﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;

namespace System
{
    /// <summary>
    /// 
    /// </summary>
    public static class ExtendClass
    {
        #region 扩展String
        /// <summary>
        /// 生成弹出提示框的Javascript
        /// </summary>
        /// <param name="str">要提示的内容</param>
        /// <returns></returns>
        public static string AlertOffPage(this string str)
        {
            return "<script>$(document).ready(function () {alert('" + str + "');});</script>";
        }
        /// <summary>
        /// 生成弹出提示框的Javascript
        /// </summary>
        /// <param name="str">要提示的内容</param>
        /// <param name="active">要进行其它操作的Javascript内容</param>
        /// <param name="isFirst">active是否要提前执行</param>
        /// <returns></returns>
        public static string AlertOffPage(this string str,string active,bool isFirst)
        {
            if (active.ToLower().IndexOf("<script>".ToLower()) >= 0)
            {
                int index1 = active.IndexOf('>');
                int index2 = active.LastIndexOf('<');
                active = active.Substring(index1 + 1, index2 - index1-1);
            }
            var value= "<script>$(document).ready(function () {alert('"+str+"');});"+active+";</script>";
            if (isFirst)
            {
                value = "<script>"+active+";$(document).ready(function () {alert('"+str+"');});</script>";
            }
            return value;
        }
        /// <summary>
        /// 重定向到此地址
        /// </summary>
        /// <param name="url">地址( -1 表示返回)</param>
        /// <param name="msg">重定向后要弹出的提示框的内容(alert)</param>
        /// <returns></returns>
        public static string Redirect(this string url, string msg)
        {
            var result = string.Format("<script>{0} {1}</script>",string.IsNullOrEmpty(msg)?"":string.Format("alert('{0}');",msg),"-1"==url?"history.go(-1);":string.Format("window.location.href={0};",string.IsNullOrEmpty(url)?"window.location.href":string.Format("'{0}'",url)));
            return result;
        }
        /// <summary>
        /// 重定向到此地址
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="msg">重定向后要弹出的提示框的内容(confirm)</param>
        /// <param name="url2">当前用户选择否是要重定向的地址</param>
        /// <returns></returns>
        public static string Redirect(this string url, string msg,string url2)
        {
            var locationUrl = "";
            var locationUrl2 = "";
            if (string.IsNullOrEmpty(url))
                locationUrl = "window.location.href";
            else
                locationUrl = "'"+url+"'";
            if (!string.IsNullOrEmpty(url2))
                locationUrl2 = string.Format("window.location.href='{0}';",url2);

            return  string.Format("<script>{0}</script>",
                    string.IsNullOrEmpty(msg)
                        ?string.Format("window.location.href={0};",locationUrl)
                        : "if(confirm('" + msg + "')){window.location.href=" + locationUrl + ";}else{" + locationUrl2 + "}");
        }
        /// <summary>
        /// 重定向到此地址
        /// </summary>
        /// <param name="url">地址</param>
        /// <returns></returns>
        public static string Redirect(this string url)
        {
            return "<script>window.location.href=" + (url == "" ? "window.location.href" : ("'" + url + "'")) + ";</script>";
        }
        
        /// <summary>
        /// 将字符串转换为int，失败时返回0
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int TryParseOrDefault(this string s)
        {
            int result;
            if (int.TryParse(s, out result))
                return result;
            return 0;
        }

        public static bool IsDouble(this string s)
        {
            double d;
            return double.TryParse(s, out d);
        }
        public static bool IsInt(this string s)
        {
            int d;
            return int.TryParse(s, out d);
        }
        public static bool IsDateTime(this string s)
        {
            DateTime d;
            return DateTime.TryParse(s, out d);
        }
        public static bool IsBool(this string s)
        {
            bool d;

            return s == null || bool.TryParse(s.Split(',')[0], out d);
        }
        public static string Format(this string str, params object[] args)
        {
            return string.Format(str, args);
        }
        #endregion
    }
    public static class NullableEx
    {
        public static string ToString<T>(this System.Nullable<T> obj, string format) where T : struct
        {
            return string.Format("{0:" + format + "}", obj);
        }
    }
}
